#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021/11/25 15:25
# @Author  : huidong.bai
# @File    : demo01.py
# @Software: PyCharm

import serial.tools.list_ports
import platform
import os


def get_serial_com():
    if platform.system() == 'Windows':
        print('运行在windows环境下')
        ports = list(serial.tools.list_ports.comports())
        port_list = []
        if len(ports) == 0:
            print("串口设备为空，请检查!")
        else:
            for each_port in ports:
                if "USB-SERIAL" in each_port[1]:
                    port_list.append(each_port[0])
        return port_list
    else:
        print('运行在非Winodws环境下')
        return os.popen('ls /dev/tty.*usbserial*').readlines()


if __name__ == '__main__':
    print(get_serial_com())
